package com.akol.seret;

import com.akol.seret.entities.Movie;
import com.akol.seret.webApiEntities.Requests.RequestClassAdapter;
import com.akol.seret.utils.GeneralUtils;
import com.akol.seret.webApiEntities.Requests.BasicRequestMessage;
import com.akol.seret.webApiEntities.Requests.MovieRequestMessage;
import com.akol.seret.utils.C;
import com.akol.seret.webApiEntities.MessageType;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gad on 18/02/2015.
 */

public class PostMethod {

    public JSONObject postData(String url, String string) {

        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(C.URL);
            URI website = new URI(url);
            request.setURI(website);

            try {

                ArrayList<MessageType> idList = new ArrayList<MessageType>();
                ArrayList<Object> moviesList = new ArrayList<Object>();

                idList.add(MessageType.Movie);
                moviesList.add(new Movie("Sagi's Mom"));
                MovieRequestMessage movieReMessage = new MovieRequestMessage(idList, moviesList);

                GsonBuilder builder = new GsonBuilder();
                Gson gson = builder.registerTypeHierarchyAdapter(
                        BasicRequestMessage.class, new RequestClassAdapter())
                        .create();
                String userRequest = gson.toJson(movieReMessage);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                nameValuePairs.add(new BasicNameValuePair("user_request",
                        userRequest));

                request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = client.execute(request);
                int status = response.getStatusLine().getStatusCode();
                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);
                    JSONObject json = new JSONObject(data);
                    return json;
                } else {
                    GeneralUtils.logError("Failed, response status: "
                            + Integer.toString(status));
                    return null;
                }
            } catch (ClientProtocolException e) {
                GeneralUtils.logError("Failed to parse JSON...");
                return null;
            } catch (JSONException e) {
                GeneralUtils.logError("Failed to parse JSON...");
                return null;
            } catch (Exception e) {
                GeneralUtils.logError("Failed...");
                return null;
            }

        } catch (Exception e) {
            return null;
        }
    }

}
